package com.shanzmoo.admin.manager.store;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shanzmoo.db.store.entity.StoreStockCheckBillDetailEntity;
import com.shanzmoo.db.store.entity.StoreStockCheckBillEntity;
import com.shanzmoo.db.store.service.IStoreStockCheckBillDetailService;
import com.shanzmoo.db.store.service.IStoreStockCheckBillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * 库存期初导入单明细 管理器
 *
 * @author fs 2021-03-09
 */
@Slf4j
@Component
public class StoreStockCheckBillDetailManager {

    @Autowired
    private IStoreStockCheckBillDetailService storeStockCheckBillDetailService;
    @Autowired
    private IStoreStockCheckBillService storeStockCheckBillService;

    public void updateById(StoreStockCheckBillDetailEntity entity) {
        StoreStockCheckBillDetailEntity db = storeStockCheckBillDetailService.getById(entity.getId());
        Assert.notNull(db,"数据不存在");
        StoreStockCheckBillEntity parent = storeStockCheckBillService.getById(db.getBillId());
        Assert.notNull(parent,"盘点单不存在");
        Assert.isTrue(parent.getStatus() == 0,"盘点单必须是审核中状态");
        storeStockCheckBillDetailService.updateById(entity);
    }

    public List<StoreStockCheckBillDetailEntity> export(Integer id) {
        QueryWrapper<StoreStockCheckBillDetailEntity> qw = Wrappers.query();
        qw.lambda().eq(StoreStockCheckBillDetailEntity::getBillId,id);
        qw.select("store_code","sku","sys_sku_num","null as actual_sku_num");
        return storeStockCheckBillDetailService.list(qw);
    }
}
